package cn.gson.crm.controller.position;

import java.util.List;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.PositionDao;
import cn.gson.crm.model.entity.Position;

/**
 * Servlet implementation class PositionFormServlet
 */
@WebServlet("/position/form")
public class PositionFormServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private String formJsp = "position/position_form.jsp";
	private PositionDao positionDao = new PositionDao();

	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String pid = request.getParameter("id");


		if (pid != null) {
			Position po = positionDao.getPositionById(1, pid);
			request.setAttribute("po", po);
		}
		getServletPathAndForward1(request, formJsp);
		//forward(formJsp);
	}

	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String pid = request.getParameter("id");
		String pname = request.getParameter("pname");
		String pdiscrible = request.getParameter("pdiscrible");
		Position po = new Position();
		po.setPname(pname);
		po.setPdiscrible(pdiscrible);

		// 如果id为空则开始创建

		if (pid == null || pid.isEmpty()) {

			if (pname == null || pname.isEmpty()) {
				request.setAttribute("error", "职位名称不能为空！");
				get(request, response);
				return;
			}else
				if (pname.trim() == null || pname.trim().isEmpty()) {
					request.setAttribute("error", "职位名称不能为空！");
					get(request, response);
					return;
				}
			if (pdiscrible == null || pdiscrible.isEmpty()) {
				request.setAttribute("error", "描述不能为空！");
				get(request, response);
				return;			
			}
			else if (pdiscrible.trim() == null || pdiscrible.trim().isEmpty()) {
				request.setAttribute("error", "描述不能为空！");
				get(request, response);
				return;			
			}
			//判断是否职位是否已经存在
			Position position1 = positionDao.getPositionByName(pname);
			if (position1 != null) {
				if (position1.getStatus() == 1) {
					request.setAttribute("error", "该职位已存在，请重新输入！");
					get(request, response);
					return;
				}else{
					po.setId(position1.getId());
					positionDao.changePosition(po,1);
				}		
			}else{
				positionDao.savaPosition(po);
			}
			redirect("position/manager");
			return;
		} else {
			// 修改职位信息
		po.setId(Long.parseLong(pid));
			if (pname == null || pname.isEmpty()) {
				request.setAttribute("error", "职位不能为空！");
				get(request, response);			
				return;
			}
			if (pdiscrible == null || pdiscrible.isEmpty()) {
				request.setAttribute("error", "职位描述不能为空！");
				get(request, response);			
				return;
			}
			// 判断该职位是否已经存在
			List<Position> positionList = positionDao.getPositionNotContainSelf(1, pid);
			if (positionList!= null) {
				for (Position position1 : positionList) {
					if (pname.equals(position1.getPname())) {
						request.setAttribute("error", "该职位已存在！请重新输入");
						get(request, response);			
						return;
					}
				}
			}	
			positionDao.changePosition(po);
			redirect("position/manager");
			return;
		}
	}


}
